package com.zhou;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * t12
 * 第一行为一个字符串S与一个非负整数m。(1 <= |S| <= 1000, 1 <= m <= 1000000)
 *
 * @author zhouxianfeng
 * @date 2021-8-23
 */
public class T12 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] s = in.nextLine().split(" ");
        char[] s1 = s[0].toCharArray();
        int m = Integer.parseInt(s[1]);
        List<Integer>[] mm = new List[26];

        for (int i = 0; i < s1.length; i++) {
            int index = s1[i] - 'a';
            if (mm[index] == null) {
                mm[index] = new ArrayList();
            }
            mm[index].add(i);
        }
        int max = 0;
        for (int i = 0; i < mm.length; i++) {
            if (mm[i] != null) {
                Collections.reverse(mm[i]);
                int[] ints = mm[i].stream().mapToInt(Integer::intValue).toArray();
                max = Math.max(max, ops(ints, m));
            }
        }
        System.out.println(max);

    }

    public static int ops(int[] list, int m) {
        int[][] vec = new int[list.length][list.length];
        for (int i = 0; i < list.length - 1; i++) {
            vec[i][i + 1] = Math.abs(list[i] - list[i + 1]) - 1;
        }
        for (int i = 2; i < list.length; i++) {
            for (int j = 0; j < list.length - i; j++) {
                int row = j;
                int col = j + i;
//        int n = list.length;
//        for (int j = 2; j < n; ++j) {
//            for (int i = 0; i < n - j; ++i) {
//                int row = i, col = i + j;
                vec[row][col] = vec[row + 1][col - 1] + Math.abs(list[row] - list[col]) - col + row;
            }
        }
        int max = 0;
        for (int i = 0; i < list.length; i++) {
            for (int j = i; j < list.length; j++) {
                if (vec[i][j] <= m) {
                    max = Math.max(max, j - i + 1);
                }
            }
        }
        return max;
    }
}
